<template>
  <div class="file-list">
    <div class="file-item" v-for="file in files" :key="file.url">
      <div style="display: flex; align-items: center;">
        <div class="file-type" :class="getFileType(file.url)">
          {{ getFileType(file.url).slice(0, 1).toUpperCase() }}
        </div>
        <div class="file-info">
          <div class="file-name">{{ file.name }}</div>
          <div class="file-meta">{{ file.size }}</div>
        </div>
      </div>

      <div class="preview-container">
        <iframe v-if="getFileType(file.url) === 'pdf'" :src="file.url" style="width:100%; height:300px"
          frameborder="0"></iframe>
        <el-image v-else-if="getFileType(file.url) === 'image'" :src="file.url" fit="contain"
          :z-index="9999"></el-image>
        <video v-else-if="getFileType(file.url) === 'video'" controls>
          <source :src="file.url" type="video/mp4">
        </video>
        <audio v-else-if="getFileType(file.url) === 'audio'" controls>
          <source :src="file.url" type="audio/mpeg">
        </audio>
        <a v-else :href="file.url" class="action-btn" download>下载文件</a>
      </div>

      <div class="actions">
        <button class="action-btn" @click="openFile(file.url)">打开</button>
        <button class="action-btn" @click="handleDownload(file.url)">下载</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router' // 新增路由钩子导入
const route = useRoute()
import { ElMessage, ElImage } from 'element-plus'
import { getFileDetail } from '../api/index'
const loading = ref(true)
onMounted(async () => {
  try {
    const shareId = route.params.shareOnlyId
    const productId = route.params.productId
    const res = await getFileDetail({
      shareId: shareId,
      productId: productId
    })
    // 修改为使用res.data.code
    if (res.code == 200) {
      files.value = res.data.map(item => ({
        name: item.productsFile.fileName,
        url: item.productsFile.fileUrl,
        type: getFileType(item.productsFile.fileUrl),
      }))
    }
  } catch (error) {
    console.error('获取文件列表失败:', error)
    ElMessage.error('获取文件列表失败')
  } finally {
    loading.value = false
  }
})
const files = ref([])

const getFileType = (url) => {
  const ext = url.split('.').pop().toLowerCase()
  const types = {
    pdf: 'pdf',
    jpg: 'image', jpeg: 'image', png: 'image', gif: 'image',
    mp4: 'video', mov: 'video', avi: 'video',
    mp3: 'audio', wav: 'audio',
  }
  return types[ext] || 'other'
}

const openFile = (url) => {
  window.open(url)
}

const handleDownload = async (url) => {
  try {
    const response = await fetch(url)
    const blob = await response.blob()
    const downloadUrl = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = downloadUrl
    link.setAttribute('download', url.split('/').pop())
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
    window.URL.revokeObjectURL(downloadUrl)
  } catch (error) {
    console.error('下载失败:', error)
    ElMessage.error('文件下载失败')
  }
}
</script>

<style scoped>
/* 基础重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f5;
  padding: 15px;
}

/* 文件列表容器 */
.file-list {
  max-width: 600px;
  margin: 0 auto;

}

/* 文件卡片样式 */
.file-item {
  background: white;
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 文件类型标识 */
.file-type {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  color: white;
}

/* 不同类型颜色 */
.pdf {
  background: #FF5252;
}

.image {
  background: #4CAF50;
}

.video {
  background: #2196F3;
}

.audio {
  background: #9C27B0;
}

.other {
  background: #607D8B;
}

/* 文件信息 */
.file-info {
  flex: 1;
}

.file-name {
  font-weight: 500;
  margin-bottom: 4px;
  color: #333;
}

.file-meta {
  font-size: 0.9em;
  color: #666;
}

/* 预览容器 */
.preview-container {
  margin-top: 15px;
  border-radius: 8px;
  overflow: hidden;
}

/* 响应式媒体 */
video,
audio {
  width: 100%;
  height: auto;
}

/* 操作按钮 */
.actions {
  margin-top: 12px;
  display: flex;
  gap: 10px;
}

.action-btn {
  flex: 1;
  padding: 8px;
  border: none;
  border-radius: 6px;
  background: #f0f0f0;
  color: #333;
  cursor: pointer;
}

/* 移动端优化 */
@media (max-width: 480px) {
  .file-item {
    padding: 12px;
  }

  .file-type {
    width: 36px;
    height: 36px;
  }
}
</style>